<?php
include_once 'common/tools.php';
//启用一个带有ob_gzip压缩机的工作台
ob_start('ob_gzip');

$fId = @$_GET["fId"];
$uId = @$_GET["uId"];
$fUId = @$_GET["fUId"];
$fState = @$_GET["fState"];
$action = @$_GET["action"];

$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysql_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"]);
mysql_select_db($config["DB_NAME"], $conn);
mysql_query("SET NAMES utf8");

if($action == "R"){
	$sql = NULL;
	if ($fState == '1') {
		$sql = "select * from( ".
				"select '1' f, f.fId, u.id uId, u.userId, u.userPic, u.userCity, IF(u.userBirthDay <> '0000-00-00', (YEAR(CURDATE())-YEAR(u.userBirthDay))-(RIGHT(CURDATE(),5)<RIGHT(u.userBirthDay,5)), '0') userAge, u.userSex, IFNULL(u.userSign,'') userSign, f.updateDate ".
				"from live_user u ,live_friend f ".
				"where  ".
				"		f.toUserId = u.id ".
				"and	f.fState = '" . $fState . "' ".
				"and	f.fromUserId = " . $uId . " ".
				"UNION ALL ".
				"select '1' f, f.fId, u.id uId, u.userId, u.userPic, u.userCity, IF(u.userBirthDay <> '0000-00-00', (YEAR(CURDATE())-YEAR(u.userBirthDay))-(RIGHT(CURDATE(),5)<RIGHT(u.userBirthDay,5)), '0') userAge, u.userSex, IFNULL(u.userSign,'') userSign, f.updateDate ".
				"from live_user u ,live_friend f ".
				"where  ".
				"		f.fromUserId = u.id ".
				"and	f.fState = '1' ".
				"and	(f.toUserId = " . $uId . " or f.toUserId = 0) ".
				")t ".
				"order by t.updateDate";
	}else if ($fState == '0') {
		$sql = "select '0' f, f.fId, u.id uId, u.userId, u.userPic, u.userCity, IF(u.userBirthDay <> '0000-00-00', (YEAR(CURDATE())-YEAR(u.userBirthDay))-(RIGHT(CURDATE(),5)<RIGHT(u.userBirthDay,5)), '0') userAge, u.userSex, IFNULL(u.userSign,'') userSign, f.updateDate ".
				"from live_user u ,live_friend f ".
				"where  ".
				"		f.fromUserId = u.id ".
				"and	f.fState = '0' ".
				"and	f.toUserId = " . $uId . " ".
				"order by f.updateDate";
	}
	$list = mysql_query($sql, $conn);
	while ($obj = mysql_fetch_object($list)) {
		$objArray[] = $obj;
	}
	//echo '{"sql":"' . $sql . '","results":' . json_encode($objArray) . '}';
	echo '{"results":' . json_encode($objArray) . '}';
}else{
	if ($fState == '0') {
		$sql = "insert into live_friend (fromUserId, toUserId, fState) values('" . $uId . "', '" . $fUId . "', '0')";
	}else if ($fState == '1') {
		$sql = "update live_friend set fState = '1' where fId = " . $fId;
	}else{
		$sql = "delete from live_friend where fId = " . $fId;
	}

	if (mysql_query($sql, $conn)) {
		echo '{"result":"success"}';
	} else {
		echo '{"sql":"' . $sql . '","result":"faild"}';
	}
}
//echo '{"results":' . json_encode($newsArray) . '}';
//输出压缩成果
ob_end_flush();
?>